//
//  TPVerticalPageViewCell.m
//  ThePaperBase
//
//  Created by liubo on 2021/8/20.
//  Copyright © 2021 scar1900. All rights reserved.
//

#import "LBVerticalPageViewCell.h"
#import <Masonry/Masonry.h>

@interface LBVerticalPageViewCell () <UIScrollViewDelegate>

{
    CGFloat topicHeadheight;
    BOOL refreshSelected;
    BOOL loadMoreSelected;
}

///翻动到上一页动画正在进行
@property (nonatomic, assign) BOOL toLastMoving;
///翻动到下一页动画正在进行
@property (nonatomic, assign) BOOL toNextMoving;
///标识符
@property (nonatomic, copy) NSString *reuseIdentifier;

@end

@implementation LBVerticalPageViewCell

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _reuseIdentifier = reuseIdentifier;
        self.toLastThrottle = 80;
        self.toNextThrottle = 80;
        [self initUI];
        [self setUpUI];
    }
    return self;
}

- (void)configContentOffset:(CGFloat)offset
{
    
}

- (void)initUI
{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

- (void)setUpUI
{
}

- (void)updateWithModel:(id)model
{
    
}

#pragma mark - UISrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(verticalPageScrollViewDidScroll:)]) {
        [self.delegate verticalPageScrollViewDidScroll:scrollView];
    }
    if (scrollView.contentOffset.y < - self.toLastThrottle) {
       
        refreshSelected = YES;
    }else{
        refreshSelected = NO;
    }
    self.refreshIndicatorView.selected = refreshSelected || self.toLastMoving;

    
    CGFloat contentHeight = MAX(self.tableView.bounds.size.height, self.tableView.contentSize.height);
    if (scrollView.contentOffset.y - self.toNextThrottle > MAX(0,contentHeight - self.tableView.bounds.size.height)) {
        loadMoreSelected = YES;
    }else{
        loadMoreSelected = NO;
    }
    self.loadMoreIndicatorView.selected = loadMoreSelected || self.toNextMoving;
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat contentHeight = MAX(self.tableView.bounds.size.height, self.tableView.contentSize.height);
    BOOL offsetIsOverCriticalValue = scrollView.contentOffset.y - self.toNextThrottle > (contentHeight - self.tableView.bounds.size.height);
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(verticalPageScrollViewDidEndDragging:willDecelerate:)]) {
        [self.delegate verticalPageScrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
    if (offsetIsOverCriticalValue) {
        if (self.next) {
            self.toNextMoving = YES;
            [UIView animateWithDuration:0.2 animations:^{
                self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
            }];
            self.next();
        }
    }
    
    if (scrollView.contentOffset.y < self.toLastThrottle) {
        if (self.toLast) {
            
            self.toLastMoving = YES;
            [UIView animateWithDuration:0.2 animations:^{
                self.tableView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);
            }];
            self.toLast();
        }
    }
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        _tableView.tableFooterView = self.loadMoreIndicatorView;
    }
    return _tableView;
}

@end
